package com.lovo.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.project.bean.JournalBean;
import com.lovo.project.bean.MessagePrompt;
import com.lovo.project.bean.UserBean;
import com.lovo.project.mapper.JournalMapper;
import com.lovo.project.service.UserService;
import com.lovo.project.util.Email;
import com.lovo.project.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.security.GeneralSecurityException;
import java.time.LocalDate;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    JournalMapper journalMapper;

    @RequestMapping("login")
    public ResponseResult<UserBean> login(String userCode, String userPassword) {
        ResponseResult<UserBean> responseResult = new ResponseResult<>();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_code", userCode);
            queryWrapper.eq("user_password", userPassword);
            UserBean userBean = userService.queryUser(queryWrapper);
            JournalBean journalBean = new JournalBean();
            LocalDate tiame = LocalDate.now();
            if (userBean == null) {
                responseResult.setCode(MessagePrompt.ACCOUNT_OR_PASSWORD_ERROR.getCode());
                responseResult.setMessage(MessagePrompt.ACCOUNT_OR_PASSWORD_ERROR.getMessage());
            } else {
                journalBean.setJournalTime(tiame);
                journalBean.setJournalContent("用户" + userBean.getUserName() + "在" + tiame + "登录系统");
                journalMapper.insert(journalBean);
                responseResult.setData(userBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(MessagePrompt.ERROR.getCode());
            responseResult.setMessage(MessagePrompt.ERROR.getMessage());
        }
        return responseResult;
    }

    @RequestMapping("/queryUserName")
    public String queryUserName(Integer userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        UserBean userBean = userService.queryUser(queryWrapper);
        return userBean.getUserName();
    }

    @RequestMapping("/queryUserList")
    public Page<UserBean> queryUserList(Integer pg, Integer pgsize, String userName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("user_name", userName);
        Page<UserBean> page = new Page<>(pg, pgsize);
        Page<UserBean> pageInfo = userService.selectUserList(page, queryWrapper);
        return pageInfo;
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(Integer userId) {
        userService.deleteUser(userId);
        return "ok";
    }

    @RequestMapping("/queryUser")
    public UserBean queryUser(Integer userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        UserBean userBean = userService.queryUser(queryWrapper);
        return userBean;
    }

    @RequestMapping("/addUser")
    public String addUser(@RequestParam("roleId") List<Integer> roleId, @RequestParam("userCode") String userCode,
                          @RequestParam("userName") String userName, @RequestParam("userPassword") String userPassword) {
        UserBean userBean = new UserBean();
        userBean.setUserCode(userCode);
        userBean.setUserName(userName);
        userBean.setUserPassword(userPassword);
        userService.addUser(userBean, roleId);

        return "ok";
    }

    @RequestMapping("/updatePassword")
    public String updatePassword(Integer userId, String oldPassword, String newPassword, String newPassword1) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        UserBean userBean1 = userService.queryUser(queryWrapper);
        UserBean userBean2 = new UserBean();
        if (oldPassword.equals(userBean1.getUserPassword())) {
            if (newPassword.equals(newPassword1) && newPassword != "") {
                userBean2.setUserId(userId);
                userBean2.setUserPassword(newPassword);
                userService.updatePassword(userBean2);
                return "ok";
            }
        }
        return "fail";
    }

    @RequestMapping("/sendEmails")
    public String sendEmails(String address, String title, String content) throws GeneralSecurityException, MessagingException {
        if (address!=null && address!="") {
            Email.email(address, title, content);
            return "ok";
        }else {
            return "error";
        }
    }
}
